home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / ButtonBase.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  20.5 KB  |  762 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.AWTException;
  4. import java.awt.Canvas;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.Event;
  8. import java.awt.Graphics;
  9. import java.awt.LayoutManager;
  10. import java.awt.Panel;
  11. import java.awt.Point;
  12. import symantec.itools.util.Timer;
  13.  
  14. /**
  15.  * ButtonBase abstract class used to implement special Symantec buttons.
  16.  * It supports InfoTips, 3-D beveled borders, and continuous notification
  17.  * messages while the button is pressed.
  18.  * @version 1.0, Nov 26, 1996
  19.  * @author Symantec
  20.  */
  21.  
  22. public abstract class ButtonBase
  23.     extends Canvas
  24. {
  25.     /**
  26.      * True if the button is currently pressed.
  27.      */
  28.     protected boolean   pressed;
  29.     /**
  30.      * True if the button has been released.
  31.      */
  32.     protected boolean   released;
  33.     /**
  34.      * True if mouse is over button.
  35.      */
  36.     protected boolean   inButton;
  37.     /**
  38.      * If true button will continuously send messages when pressed.
  39.      */
  40.     protected boolean   notifyWhilePressed;
  41.     /**
  42.      * If true an InfoTip will appear after mouse has been over button a few moments.
  43.      */
  44.     protected boolean   showInfoTip;
  45.     /**
  46.      * The notify timer is running.
  47.      */
  48.     protected boolean   running;
  49.     /**
  50.      * Notification has been sent in response to a mouse down.
  51.      */
  52.     protected boolean   notified;
  53.     /**
  54.      * Show the focus when the mouse enters the button.
  55.      */
  56.     protected boolean   showFocus;
  57.     /**
  58.      * Draw InfoTip during paint().
  59.      */
  60.     protected boolean   doInfoTip;
  61.     /**
  62.      * The 3-D bevel height of the button in pixels.
  63.      */
  64.     protected int       bevel;
  65.     /**
  66.      * The delay in milliseconds between notifications when the button pressed.
  67.      */
  68.     protected int       notifyDelay;
  69.     /**
  70.      * The delay in milliseconds till InfoTips first appear.
  71.      */
  72.     protected int       infoTipDelay;
  73.     /**
  74.      * Adjustment for 3-D bevel while button is pressed.
  75.      */
  76.     protected int       pressedAdjustment;
  77.     /**
  78.      * The text to display in the InfoTip.
  79.      */
  80.     protected String    infoTipText;
  81.     /**
  82.      * The color of the text in the InfoTip.
  83.      */
  84.     protected Color     infoTipTextColor;
  85.     /**
  86.      * Timer used to time notification messages while button pressed.
  87.      */
  88.     protected Timer     notifyTimer;
  89.     /**
  90.      * Timer used to time delay before InfoTips appear.
  91.      */
  92.     protected Timer     infoTipTimer;
  93.     /**
  94.      * Location to display the InfoTip, x-coordinate.
  95.      */
  96.     protected int       infoTipX;
  97.     /**
  98.      * Location to display the InfoTip, y-coordinate.
  99.      */
  100.     protected int       infoTipY;
  101.     /**
  102.      * Layout manager for InfoTips.
  103.      */
  104.     protected LayoutManager infoTipLayoutManager;
  105.  
  106.     /**
  107.      * Constructs a default ButtonBase. The defaults are no notifyWhilePressed,
  108.      * no InfoTips, and a bevel height of 1.
  109.      */
  110.     protected ButtonBase()
  111.     {
  112.         pressed            = false;
  113.         released           = true;
  114.         notifyWhilePressed = false;
  115.         showInfoTip        = false;
  116.         running            = false;
  117.         notified           = false;
  118.         notifyTimer        = null;
  119.         infoTipTimer       = null;
  120.         infoTipTextColor   = Color.black;
  121.         notifyDelay        = 1000;
  122.         infoTipDelay       = 1000;
  123.         bevel              = 1;
  124.         pressedAdjustment  = 0;
  125.         resize(10, 10);
  126.     }
  127.  
  128.     /**
  129.      * Sets the 3-D bevel height of the button.
  130.      * @param size size of bevel, in pixels
  131.      * @see #getBevelHeight
  132.      */
  133.     public void setBevelHeight(int size)
  134.         // throws AWTException
  135.     {
  136.         try
  137.         {
  138.             checkBevelSize(size);
  139.         }
  140.         catch(AWTException e)
  141.         {
  142.             System.err.println("Invalid Bevel Size " + size);
  143.         }
  144.  
  145.         bevel = size;
  146.  
  147.         invalidate();
  148.     }
  149.  
  150.     /**
  151.      * Returns the current 3-D bevel height of the button, in pixels.
  152.      * @see #setBevelHeight
  153.      */
  154.     public int getBevelHeight()
  155.     {
  156.         return bevel;
  157.     }
  158.  
  159.     /**
  160.      * Sets whether the button will continually send notify messages while pressed.
  161.      * @param f true = send messages; false = do not send messages
  162.      * @see #getNotifyWhilePressed
  163.      * @see #setNotifyDelay
  164.      * @see #getNotifyDelay
  165.      */
  166.     public void setNotifyWhilePressed(boolean f)
  167.     {
  168.         notifyWhilePressed = f;
  169.  
  170.         if (notifyWhilePressed)
  171.         {
  172.             notifyTimer = new Timer(this, notifyDelay, true, Event.ACTION_EVENT);
  173.         }
  174.         else if (notifyTimer != null)
  175.         {
  176.             notifyTimer = null;
  177.         }
  178.     }
  179.  
  180.     /**
  181.      * Returns the current notifyWhilePressed status.
  182.      * @see #setNotifyWhilePressed
  183.      * @see #setNotifyDelay
  184.      * @see #getNotifyDelay
  185.      */
  186.     public boolean getNotifyWhilePressed()
  187.     {
  188.         return notifyWhilePressed;
  189.     }
  190.  
  191.     /**
  192.      * Sets the notification message delay in milliseconds.
  193.      * @param d the delay between notification messages in milliseconds
  194.      * @see #setNotifyWhilePressed
  195.      * @see #getNotifyDelay
  196.      */
  197.     public void setNotifyDelay(int d)
  198.     {
  199.         notifyDelay = d;
  200.     }
  201.  
  202.     /**
  203.      * Returns the current delay (in milliseconds) between notification messages.
  204.      * @see #setNotifyWhilePressed
  205.      * @see #setNotifyDelay
  206.      */
  207.     public int getNotifyDelay()
  208.     {
  209.         return notifyDelay;
  210.     }
  211.  
  212.     /**
  213.      * Sets whether an InfoTip is displayed for this button.
  214.      * @param f If true an InfoTip will be displayed for this button
  215.      * @see #getShowInfoTip
  216.      * @see #setInfoTipDelay
  217.      * @see #getInfoTipDelay
  218.      * @see #setInfoTipText
  219.      * @see #getInfoTipText
  220.      * @see #getInfoTipTextColor
  221.      * @see #setInfoTipTextColor
  222.      */
  223.     public void setShowInfoTip(boolean f)
  224.     {
  225.         showInfoTip = f;
  226.  
  227.         if (showInfoTip)
  228.         {
  229.             infoTipTimer = new Timer(this, notifyDelay, true, Event.ACTION_EVENT);
  230.         }
  231.         else if (infoTipTimer != null)
  232.         {
  233.             infoTipTimer = null;
  234.         }
  235.     }
  236.  
  237.     /**
  238.      * Returns whether an InfoTip is displayed for this button.
  239.      * @see #setShowInfoTip
  240.      * @see #setInfoTipDelay
  241.      * @see #getInfoTipDelay
  242.      * @see #setInfoTipText
  243.      * @see #getInfoTipText
  244.      * @see #getInfoTipTextColor
  245.      * @see #setInfoTipTextColor
  246.      */
  247.     public boolean getShowInfoTip()
  248.     {
  249.         return showInfoTip;
  250.     }
  251.  
  252.     /**
  253.      * Sets the delay till an InfoTip is displayed for this button.
  254.      * @param d the delay, in milliseconds, from the time the mouse enters the
  255.      * button till the InfoTip is displayed
  256.      * @see #setShowInfoTip
  257.      * @see #getShowInfoTip
  258.      * @see #getInfoTipDelay
  259.      * @see #setInfoTipText
  260.      * @see #getInfoTipText
  261.      * @see #getInfoTipTextColor
  262.      * @see #setInfoTipTextColor
  263.      */
  264.     public void setInfoTipDelay(int d)
  265.     {
  266.         infoTipDelay = d;
  267.     }
  268.  
  269.     /**
  270.      * Returns the delay, in milliseconds, from the time the mouse enters the
  271.      * button till the InfoTip is displayed
  272.      * @see #setShowInfoTip
  273.      * @see #getShowInfoTip
  274.      * @see #setInfoTipDelay
  275.      * @see #setInfoTipText
  276.      * @see #getInfoTipText
  277.      * @see #getInfoTipTextColor
  278.      * @see #setInfoTipTextColor
  279.      */
  280.     public int getInfoTipDelay()
  281.     {
  282.         return infoTipDelay;
  283.     }
  284.  
  285.     /**
  286.      * Sets whether this button will be shown as having the focus when the mouse enters.
  287.      * @param boolean f true = show at mouse enter; false = do not show at mouse enter
  288.      * @see #getShowFocus
  289.      */
  290.     public void setShowFocus(boolean f)
  291.     {
  292.         showFocus = f;
  293.     }
  294.  
  295.     /**
  296.      * Returns whether this button will be shown as having the focus when the mouse enters.
  297.      * @see #setShowFocus
  298.      */
  299.     public boolean getShowFocus()
  300.     {
  301.         return showFocus;
  302.     }
  303.  
  304.     /**
  305.      * Sets the text displayed in this button's InfoTip.
  306.      * @param t the text to display in the InfoTip
  307.      * @see #setShowInfoTip
  308.      * @see #getShowInfoTip
  309.      * @see #setInfoTipDelay
  310.      * @see #getInfoTipDelay
  311.      * @see #getInfoTipText
  312.      * @see #getInfoTipTextColor
  313.      * @see #setInfoTipTextColor
  314.      */
  315.     public void setInfoTipText(String t)
  316.     {
  317.         infoTipText = t;
  318.     }
  319.  
  320.     /**
  321.      * Returns the text displayed in this button's InfoTip.
  322.      * @see #setShowInfoTip
  323.      * @see #getShowInfoTip
  324.      * @see #setInfoTipDelay
  325.      * @see #getInfoTipDelay
  326.      * @see #setInfoTipText
  327.      * @see #getInfoTipTextColor
  328.      * @see #setInfoTipTextColor
  329.      */
  330.     public String getInfoTipText()
  331.     {
  332.         return infoTipText;
  333.     }
  334.  
  335.     /**
  336.      * Sets the color of text displayed in this button's InfoTip.
  337.      * @param c the color of the text displayed in the InfoTip
  338.      * @see #setShowInfoTip
  339.      * @see #getShowInfoTip
  340.      * @see #setInfoTipDelay
  341.      * @see #getInfoTipDelay
  342.      * @see #setInfoTipText
  343.      * @see #getInfoTipText
  344.      * @see #getInfoTipTextColor
  345.      */
  346.     public void setInfoTipTextColor(Color c)
  347.     {
  348.         infoTipTextColor = c;
  349.     }
  350.  
  351.     /**
  352.      * Returns the color of text displayed in this button's InfoTip.
  353.      * @see #setShowInfoTip
  354.      * @see #getShowInfoTip
  355.      * @see #setInfoTipDelay
  356.      * @see #getInfoTipDelay
  357.      * @see #setInfoTipText
  358.      * @see #getInfoTipText
  359.      * @see #setInfoTipTextColor
  360.      */
  361.     public Color getInfoTipTextColor()
  362.     {
  363.         return infoTipTextColor;
  364.     }
  365.  
  366.     /**
  367.      * Processes MOUSE_UP events.
  368.      * This is a standard Java AWT method which gets called by the AWT
  369.      * method handleEvent() in response to receiving a MOUSE_UP
  370.      * event. These events occur when the mouse button is released while 
  371.      * inside this component.
  372.      * If the notification timer is running it is stopped.
  373.      * If the mouse was pressed inside the button then an event is posted.
  374.      * 
  375.      * @param e the event
  376.      * @param x the component-relative horizontal coordinate of the mouse
  377.      * @param y the component-relative vertical coordinate of the mouse
  378.      * 
  379.      * @return always true since the event was handled
  380.      * 
  381.      * @see #mouseDown
  382.      * @see java.awt.Component#handleEvent
  383.      */
  384.     public boolean mouseUp(Event e, int x, int y)
  385.     {
  386.         if (running)
  387.         {
  388.             running = false;
  389.             notifyTimer.stop();
  390.         }
  391.  
  392.         if (pressed)
  393.         {
  394.             pressed = false;
  395.             pressedAdjustment = 0;
  396.  
  397.             if (!notifyWhilePressed || !notified)
  398.             {
  399.                 postEvent(new Event(this, Event.ACTION_EVENT, null));
  400.             }
  401.         }
  402.  
  403.         released = true;
  404.         repaint();
  405.  
  406.         return true;
  407.     }
  408.  
  409.     /**
  410.      * Processes MOUSE_DOWN events.
  411.      * This is a standard Java AWT method which gets called by the AWT
  412.      * method handleEvent() in response to receiving a MOUSE_DOWN
  413.      * event. These events occur when the mouse button is pressed while
  414.      * inside this component.
  415.      * If the notifyWhilePressed flag is true the notification Timer is started
  416.      * 
  417.      * @param e the event
  418.      * @param x the component-relative horizontal coordinate of the mouse
  419.      * @param y the component-relative vertical coordinate of the mouse
  420.      * 
  421.      * @return always true since the event was handled
  422.      * 
  423.      * @see #mouseUp
  424.      * @see java.awt.Component#handleEvent
  425.      * @see #setNotifyWhilePressed
  426.      * @see #setNotifyDelay
  427.      */
  428.     public boolean mouseDown(Event e, int x, int y)
  429.     {
  430.         if (notifyWhilePressed && !running)
  431.         {
  432.             running     = true;
  433.             notifyTimer.start();
  434.         }
  435.  
  436.         pressed           = true;
  437.         released          = false;
  438.         pressedAdjustment = bevel;
  439.         repaint();
  440.  
  441.         return true;
  442.     }
  443.  
  444.     /**
  445.      * Processes MOUSE_ENTER events.
  446.      * This is a standard Java AWT method which gets called by the AWT
  447.      * method handleEvent() in response to receiving a MOUSE_ENTER
  448.      * event. These events occur when the mouse first moves over this
  449.      * component.
  450.      * 
  451.      * @param e the event
  452.      * @param x the component-relative horizontal coordinate of the mouse
  453.      * @param y the component-relative vertical coordinate of the mouse
  454.      * 
  455.      * @return always true since the event was handled
  456.      * 
  457.      * @see #mouseExit
  458.      * @see java.awt.Component#handleEvent
  459.      */
  460.     public boolean mouseEnter(Event e, int x, int y)
  461.     {
  462.         inButton = true;
  463.  
  464.         if (showInfoTip)
  465.         {
  466.             infoTipX = x;
  467.             infoTipY = y;
  468.             infoTipTimer.start();
  469.         }
  470.  
  471.         if (!released)
  472.         {
  473.             mouseDown(e, x, y);
  474.         }
  475.  
  476.         //repaint();
  477.  
  478.         return true;
  479.     }
  480.  
  481.     /**
  482.      * Processes MOUSE_EXIT events.
  483.      * This is a standard Java AWT method which gets called by the AWT
  484.      * method handleEvent() in response to receiving a MOUSE_EXIT
  485.      * event. These events occur when the mouse first leaves this 
  486.      * component.
  487.      * 
  488.      * @param e the event
  489.      * @param x the component-relative horizontal coordinate of the mouse
  490.      * @param y the component-relative vertical coordinate of the mouse
  491.      * 
  492.      * @return always true since the event was handled
  493.      * 
  494.      * @see #mouseEnter
  495.      * @see java.awt.Component#handleEvent
  496.      */
  497.     public boolean mouseExit(Event e, int x, int y)
  498.     {
  499.         inButton = false;
  500.  
  501.         if (showInfoTip)
  502.         {
  503.             Panel infoTipPanel;
  504.  
  505.             infoTipTimer.stop();
  506.             infoTipPanel = InfoTipManager.getInfoTipPanel();
  507.             infoTipPanel.getParent().setLayout(infoTipLayoutManager);
  508.             infoTipPanel.hide();
  509.         }
  510.  
  511.         if (pressed)
  512.         {
  513.             pressed           = false;
  514.             pressedAdjustment = 0;
  515.         }
  516.  
  517.         //repaint();
  518.  
  519.         return true;
  520.     }
  521.  
  522.     /**
  523.      * Handles internal actions for this component.
  524.      * This is a standard Java AWT method which usually gets called by the AWT
  525.      * method handleEvent() in response to receiving an ACTION_EVENT event. In those 
  526.      * cases the o parameter contains the value in the event's arg field.
  527.      * 
  528.      * @param e the event that caused this action
  529.      * @param o the action
  530.      * @return true if the action was handled
  531.      * @see java.awt.Component#handleEvent
  532.      */
  533.     public boolean action(Event e, Object o)
  534.     {
  535.         if (notifyWhilePressed)
  536.         {
  537.             if (e.target == notifyTimer && !symantec.beans.Beans.isDesignTime())
  538.             {
  539.                 postEvent(new Event(this, Event.ACTION_EVENT, null));
  540.                 return true;
  541.             }
  542.         }
  543.  
  544.         if (showInfoTip)
  545.         {
  546.             if (e.target == infoTipTimer)
  547.             {
  548.                 doInfoTip = true;
  549.                 repaint();
  550.                 infoTipTimer.stop();
  551.  
  552.                 return true;
  553.             }
  554.         }
  555.  
  556.         return super.action(e, o);
  557.     }
  558.  
  559.     /**
  560.      * Enables this component so that it will respond to user input.
  561.      * This is a standard Java AWT method which gets called to enable 
  562.      * this component. Once enabled this component will respond to user input.
  563.      * 
  564.      * @see #disable
  565.      */
  566.     public void enable()
  567.     {
  568.         if (!isEnabled())
  569.         {
  570.             super.enable();
  571.             pressed = false;
  572.             pressedAdjustment = 0;
  573.         }
  574.  
  575.         repaint();
  576.     }
  577.  
  578.     /**
  579.      * Disables this component so that it doesn't respond to user input.
  580.      * This is a standard Java AWT method which gets called to disable
  581.      * this component. Once disabled this component will not respond to user 
  582.      * input.
  583.      *
  584.      * @see #enable
  585.      */
  586.     public void disable()
  587.     {
  588.         if (isEnabled())
  589.         {
  590.             super.disable();
  591.  
  592.             if (notifyTimer != null)
  593.             {
  594.                 notifyTimer.stop();
  595.             }
  596.  
  597.             if (infoTipTimer != null)
  598.             {
  599.                 infoTipTimer.stop();
  600.             }
  601.  
  602.             pressed = false;
  603.             pressedAdjustment = 0;
  604.         }
  605.  
  606.         repaint();
  607.     }
  608.  
  609.     /**
  610.      * Handles redrawing of this component on the screen.
  611.      * This is a standard Java AWT method which gets called by the Java
  612.      * AWT (repaint()) to handle repainting this component on the screen.
  613.      * The graphics context clipping region is set to the bounding rectangle
  614.      * of this component and its <0,0> coordinate is this component's 
  615.      * top-left corner.
  616.      * Typically this method paints the background color to clear the
  617.      * component's drawing space, sets graphics context to be the foreground
  618.      * color, and then calls paint() to draw the component.
  619.      *
  620.      * It is overridden here to prevent the flicker associated with the standard 
  621.      * update() method's repainting of the background before painting the component
  622.      * itself.
  623.      *
  624.      * @param g the graphics context
  625.      * @see java.awt.Component#repaint
  626.      * @see #paint
  627.      */
  628.     public void update(Graphics g)
  629.     {
  630.         Dimension s;
  631.  
  632.         s = size();
  633.  
  634.         g.clipRect(0, 0, s.width, s.height);
  635.         paint(g);
  636.     }
  637.  
  638.     /**
  639.      * Paints this component using the given graphics context.
  640.      * This is a standard Java AWT method which typically gets called
  641.      * by the AWT to handle painting this component. It paints this component
  642.      * using the given graphics context. The graphics context clipping region
  643.      * is set to the bounding rectangle of this component and its <0,0>
  644.      * coordinate is this component's top-left corner.
  645.      * 
  646.      * @param g the graphics context used for painting
  647.      * @see java.awt.Component#repaint
  648.      * @see #update
  649.      */
  650.     public void paint(Graphics g)
  651.     {
  652.         Dimension s;
  653.         int width;
  654.         int height;
  655.         int x;
  656.         int y;
  657.         int w;
  658.         int h;
  659.         int i;
  660.  
  661.         s      = size();
  662.         width  = s.width;
  663.         height = s.height;
  664.         x      = bevel  + 1;
  665.         y      = bevel  + 1;
  666.         w      = width  - 1;
  667.         h      = height - 1;
  668.  
  669.         g.setColor(Color.lightGray);
  670.         g.fillRect(0, 0, width, height);
  671.  
  672.         if (pressed)
  673.         {
  674.             y = x += bevel > 0 ? 2 : 1;
  675.             g.setColor(Color.lightGray);
  676.  
  677.             for(i = 1; i < bevel + 1; i++)
  678.             {
  679.                 g.drawLine(i, h - i, w - i, h - i);
  680.                 g.drawLine(w - i, h - i, w - i, i);
  681.             }
  682.  
  683.             g.setColor(Color.gray);
  684.  
  685.             for(i = 1; i < bevel + 1; ++i)
  686.             {
  687.                 g.drawLine(i, h, i, i);
  688.                 g.drawLine(i, i, w, i);
  689.             }
  690.         }
  691.         else
  692.         {
  693.             g.setColor(Color.white);
  694.  
  695.             for(i = 1; i < bevel + 1; i++)
  696.             {
  697.                 g.drawLine(i, h - i, i, i);
  698.                 g.drawLine(i, i, w - i, i);
  699.             }
  700.  
  701.             g.setColor(Color.gray);
  702.  
  703.             for(i = 1; i < bevel + 2; ++i)
  704.             {
  705.                 g.drawLine(i, h - i, w - i, h - i);
  706.                 g.drawLine(w - i, h - i, w - i, i);
  707.             }
  708.         }
  709.  
  710.         g.setColor(Color.black);
  711.         g.drawLine(1, 0, width - 2, 0);
  712.         g.drawLine(0, 1, 0, height - 2);
  713.         g.drawLine(1, height - 1, width - 2, height - 1);
  714.         g.drawLine(width - 1, height - 2, width - 1, 1);
  715.  
  716.         if(showInfoTip)
  717.         {
  718.             if (doInfoTip)
  719.             {
  720.                 drawInfoTip();
  721.             }
  722.         }
  723.     }
  724.  
  725.     /**
  726.      * Determines where to draw this button's InfoTip then draws it.
  727.      */
  728.     protected void drawInfoTip()
  729.     {
  730.         Panel infoTipPanel;
  731.         Point p;
  732.  
  733.         doInfoTip = false;
  734.         p = location();
  735.  
  736.         infoTipPanel = InfoTipManager.getInfoTipPanel();
  737.  
  738.         infoTipX += p.x;
  739.         infoTipY += p.y;
  740.  
  741.         infoTipLayoutManager = infoTipPanel.getParent().getLayout();
  742.         InfoTipManager.draw(infoTipX, infoTipY, infoTipText, getFontMetrics(getFont()), Color.yellow, Color.black);
  743.     }
  744.  
  745.     /**
  746.      * Ensures the given bevel size is valid for this button.
  747.      */
  748.     private void checkBevelSize(int i)
  749.         throws AWTException
  750.     {
  751.         Dimension s;
  752.  
  753.         s = size();
  754.  
  755.         if (i < 0 || i >= (s.width / 2) || i >= (s.height / 2))
  756.         {
  757.             throw new AWTException("invalid bevel size");
  758.         }
  759.     }
  760. }
  761.  
  762.